package cn.shyouchi.wuguanjia.android.ui.reportstatistic;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ReportDayStatisticActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_dates)
    RecyclerView mRvDates;

    AttendanceApi mAttendanceApi;


    String date;

    ReportDayStatisticAdapter mReportDayStatisticAdapter;

    List<ReportDayStatisticEntity> mReportDayStatisticEntities = new ArrayList<>();


    String projectId;



    @Override
    public int initContentView() {
        return R.layout.activity_report_day_statistic;
    }

    @Override
    public void initInjector() {
        date = getIntent().getStringExtra("date");

        mAttendanceApi = new AttendanceApi(mContext);
        projectId = getIntent().getStringExtra("projectId");

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mReportDayStatisticAdapter = new ReportDayStatisticAdapter(mReportDayStatisticEntities);
        mRvDates.setAdapter(mReportDayStatisticAdapter);
        mRvDates.setLayoutManager(new LinearLayoutManager(mContext));
        fetchReportDetail();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchReportDetail() {
        mAttendanceApi.getReportStatisticInfo(date, projectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<ReportDayStatisticEntity>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<ReportDayStatisticEntity>> result) {
                        mReportDayStatisticEntities.clear();
                        mReportDayStatisticEntities.addAll(result.getResultMessage());
                        mReportDayStatisticAdapter.notifyDataSetChanged();
                    }
                });

    }

}
